// 处理分支语句，如if/else/switch
export default class BranchHandler {
  init(_, tag, ...args) {
    // console.log(this)
    if (!tag) return this.init.bind(this)
    if (tag in this) {
      return this[tag](...args)
    } else if (typeof args[0] == 'function') {
      this[tag] = args[0]
    }
    return this.init.bind(this)
  }
}

// 示例一
let b = new BranchHandler().init
  `${'情况1'}${(...args) => {console.log('情况1', args)}}`
  `${'情况3'}${(...args) => {console.log('情况3', args)}}`
  `${'情况4'}${(...args) => {console.log('情况4', args)}}`

b`${'情况3'}${'app'}`

// 示例二
let comput = new BranchHandler().init
  `${'+'}${(...args) => {
    console.log('+', args)
    return args.reduce((a, b) => {a+=b; return a})
  }}`
  `${'-'}${(...args) => {
    console.log('-', args)
    return args.reduce((a, b) => {a-=b; return a})
  }}`
  `${'*'}${(...args) => {
    console.log('*', args)
    return args.reduce((a, b) => {a*=b; return a})
  }}`
  `${'/'}${(...args) => {
    console.log('/', args)
    return args.reduce((a, b) => {a/=b; return a})
  }}`

comput`${'+'}${1}${3}${5}`
